<template>
  <header>
    <div class="header-top">
      <div class="header-wrap">
        <div class="header-user">
          <span>尚品汇欢迎您！</span>
          <span v-if="name">
            <span>{{ name }}</span>
            <button @click="handleLogout">退出登录</button>
          </span>
          <!-- 
            路由跳转的方式：
              1. 路由链接跳转 router-link
              2. 编程式导航 this.$router.push
                只需要跳转路由： router-link
                跳转路由之前要做一些其他事：编程式导航
           -->

          <span v-else>请 <router-link to="/login">登录</router-link> | <router-link to="/register">免费注册</router-link></span>
        </div>
        <ul class="header-nav">
          <li><router-link to="/center">我的订单</router-link></li>
          <li><router-link to="/shopcart">我的购物车</router-link></li>
          <li>我的尚品汇</li>
          <li>尚品汇会员</li>
          <li>企业采购</li>
          <li>关注尚品汇</li>
          <li>合作招商</li>
          <li>商家后台</li>
        </ul>
      </div>
    </div>
    <div class="header-bottom header-wrap">
      <router-link to="/home">
        <img src="./images/logo.png" alt="logo" class="header-logo" />
      </router-link>
      <form @submit.prevent="toSearch">
        <input type="text" class="header-input" v-model="keyword" />
        <button class="header-btn">搜索</button>
      </form>
      <!-- <form>
        <input type="text" class="header-input" />
        <button class="header-btn" @click.prevent="toSearch">搜索</button>
      </form> -->
    </div>
  </header>
</template>

<script>
import { mapState, mapActions } from "vuex";

export default {
  name: "XHeader",
  data() {
    return {
      // 一上来有keyword参数
      // keyword: this.$route.query.keyword,
      keyword: "",
    };
  },
  methods: {
    ...mapActions("user", ["logout"]),
    toSearch() {
      // 没有keyword，不需要携带query参数
      const keyword = this.keyword;

      // const query = this.$route.query // 直接修改原对象，不行
      const query = {
        ...this.$route.query,
      };

      delete query.keyword; // 删除keyword属性

      if (keyword) {
        query.keyword = keyword;
      }

      // 问题：重复跳转到同一个地址，会出现一个错误：NavigationDuplicated: Avoided redundant navigation to current location: "/search".
      // 解决：1. this.$router.history.push  2. 无视（因为错误不影响功能）
      this.$router.push({
        name: "Search",
        query,
      });
    },
    async handleLogout() {
      await this.logout();
      this.$router.push("/");
    },
  },
  watch: {
    // $route(route) {
    //   // 当路由跳转时，给keyword重新赋值
    //   this.keyword = route.query.keyword;
    // },
    $route: {
      handler(route) {
        // 当路由跳转时，给keyword重新赋值
        this.keyword = route.query.keyword;
      },
      immediate: true,
    },
  },
  computed: {
    ...mapState({
      name: (state) => state.user.name,
    }),
  },
};
</script>

<style>
.header-top {
  height: 30px;
  background-color: #eaeaea;
}
.header-wrap {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 1200px;
  height: 100%;
  margin: 0 auto;
}
.header-nav {
  display: flex;
}
.header-nav li {
  border-right: 1px solid #b3aeae;
  padding-right: 10px;
  margin-right: 10px;
  cursor: pointer;
}
.header-nav li:last-child {
  border-right: none;
  padding-right: 0;
  margin-right: 0;
}
.header-bottom {
  height: 100px;
}
.header-form {
  display: flex;
  align-items: center;
}
.header-input {
  width: 500px;
  height: 30px;
  border: 2px solid #ea4a36;
  outline: none;
  padding: 0 5px;
}
.header-btn {
  width: 80px;
  height: 34px;
  border: none;
  background-color: #ea4a36;
  color: #fff;
  cursor: pointer;
}
</style>
